let gulp = require("gulp");
let connect = require("gulp-connect");
let proxy = require('http-proxy-middleware')
var sass = require('gulp-sass');
sass.compiler = require('node-sass');

let { paths, proxyList } = require("./config");

function devStyles() {
      return gulp.src(paths.styles.src)
            .pipe(sass().on('error', sass.logError))
            .pipe(gulp.dest(paths.styles.dist))
            .pipe(connect.reload());
}
function devScripts() {
      return gulp.src(paths.scripts.src)
            .pipe(gulp.dest(paths.scripts.dist))
            .pipe(connect.reload());
}
function devHtml() {
      return gulp.src(paths.html.src)
            .pipe(gulp.dest(paths.html.dist))
            .pipe(connect.reload());
}
function php() {
      return gulp.src(paths.php.src)
            .pipe(gulp.dest(paths.php.dist))
}

function useconnect() {
      connect.server({
            root: "./dist",
            port: 8888,
            livereload: true,
            middleware: function (connect, opt) {
                  return [
                        proxy('/phpmelon', {
                              target: 'http://localhost/jiuxian/dist/php/login.php',
                              changeOrigin: true,
                              pathRewrite: {
                                    '^/phpmelon': ''
                              }
                        }
                        ),
                        proxy('/phpregister', {
                              target: 'http://localhost/jiuxian/dist/php/register.php',
                              changeOrigin: true,
                              pathRewrite: {
                                    '^/phpregister': ''
                              }
                        }
                        ),
                        proxy('/qy', {
                              target: 'http://localhost/jiuxian/dist/php/registerqy.php',
                              changeOrigin: true,
                              pathRewrite: {
                                    '^/qy': ''
                              }
                        }
                        ),
                  ]
            }
      });
}

function watch() {
      gulp.watch(paths.styles.src, devStyles)
      gulp.watch(paths.scripts.src, devScripts)
      gulp.watch(paths.html.src, devHtml)
      gulp.watch(paths.php.src, php);
}


let dev = gulp.series(gulp.parallel(useconnect, watch));

module.exports = dev; 
